package com.ruoyi.mobile.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.security.utils.DictUtils;
import com.ruoyi.mobile.domain.ShCourseEntity;
import com.ruoyi.mobile.mapper.IShCourseMapper;
import com.ruoyi.mobile.utils.NftResponse;
import com.ruoyi.mobile.utils.Paginator;
import com.ruoyi.mobile.utils.QueryUtil;
import com.ruoyi.mobile.utils.ResponseConstants;
import com.ruoyi.system.api.domain.SysDictData;
import com.ruoyi.system.api.req.ShCourseReq;
import com.ruoyi.system.api.resp.ShCourseResp;
import org.springframework.stereotype.Service;
import com.ruoyi.mobile.service.IShCourseService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 课程信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-07-18
 */
@Service
public class ShCourseServiceImpl extends ServiceImpl<IShCourseMapper, ShCourseEntity> implements IShCourseService {

    @Override
    public NftResponse<Paginator<ShCourseResp>> getCourseList(ShCourseReq query) {
        if (query.getPageNo() == null || query.getPageNo() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "入参页码错误", null);
        }
        if (query.getPageSize() == null || query.getPageSize() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "入参每页数量错误", null);
        }
        List<SysDictData> dictLList = DictUtils.getDictCache("course_label");
        HashMap<String, String> dictLMap = new HashMap<>();
        for (SysDictData data:dictLList) {
            dictLMap.put(data.getDictValue(),data.getDictLabel());
        }
        Paginator<ShCourseResp> paginator = new Paginator<ShCourseResp>(query.getPageNo(), query.getPageSize(), 0, new ArrayList<>());
        QueryWrapper<ShCourseEntity> wrapperQuery = new QueryWrapper<>();
        if (null!=query.getShopId()) {
            wrapperQuery.eq(QueryUtil.camelToUnderline("shopId"), query.getShopId());
        }
        if (null!=query.getTeacherId()) {
            wrapperQuery.eq(QueryUtil.camelToUnderline("teacherId"), query.getTeacherId());
        }
        wrapperQuery.orderByAsc("order_num");
        IPage<ShCourseEntity> page = new Page<>(query.getPageNo(),query.getPageSize());
        IPage<ShCourseEntity> pageRtn = this.baseMapper.selectPage(page,wrapperQuery);
        List<ShCourseEntity> list = pageRtn.getRecords();
        List<ShCourseResp> result = BeanUtil.copyToList(list, ShCourseResp.class);
        for (ShCourseResp obj:result) {
            StringBuffer labelName = new StringBuffer();
            if (null != obj.getCourseLabel() && !"".equals(obj.getCourseLabel())) {
                String[] labelIdAry = obj.getCourseLabel().split(",");
                for (String labelId : labelIdAry) {
                    labelName.append(dictLMap.get(labelId)).append(",");
                }
            }
            obj.setCourseLabelNames(labelName.toString());
        }
        paginator.setPageNo(query.getPageNo());
        paginator.setPageSize(query.getPageSize());
        paginator.setTotalDataNum(pageRtn.getTotal());
        paginator.setPageData(result);
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "获取成功", paginator);
    }

    @Override
    public NftResponse<ShCourseResp> getCourseInfo(Long id) {
        ShCourseEntity entity = this.baseMapper.selectById(id);
        ShCourseResp rtn = BeanUtil.copyProperties(entity, ShCourseResp.class);
        List<SysDictData> dictLList = DictUtils.getDictCache("course_label");
        HashMap<String, String> dictLMap = new HashMap<>();
        for (SysDictData data:dictLList) {
            dictLMap.put(data.getDictValue(),data.getDictLabel());
        }
        StringBuffer labelName = new StringBuffer();
        if (null != rtn.getCourseLabel() && !"".equals(rtn.getCourseLabel())) {
            String[] labelIdAry = rtn.getCourseLabel().split(",");
            for (String labelId : labelIdAry) {
                labelName.append(dictLMap.get(labelId)).append(",");
            }
        }
        rtn.setCourseLabelNames(labelName.toString());
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "查询成功", rtn);
    }
}